#通用组件 浏览器和驱动器的开发
from selenium import webdriver
from web.config.config import Config
class Driver:
   
    @classmethod
    def get_driver(cls):
        config = Config()
        timeout = config.waitTime
        browerType = config.browerType.strip().lower()

        if browerType == "chrome":
            # 本地chrome浏览器
            chrome_options = webdriver.ChromeOptions()

            chrome_options.add_argument('--disable-gpu')
            driver = webdriver.Chrome(options=chrome_options)
            driver.maximize_window()
            driver.implicitly_wait(int(timeout))

        elif browerType == "firefox":
            # 本地firefox浏览器
            driver = webdriver.Firefox()
            driver.maximize_window()
            driver.implicitly_wait(timeout)

        elif browerType == "chrome-headless":
            # chrome headless模式
            chrome_options = webdriver.ChromeOptions()
            chrome_options.add_argument("--headless")
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument("--window-size=1920x1080")
            driver = webdriver.Chrome(options=chrome_options)

        elif browerType == "firefox-headless":
            # firefox headless模式
            firefox_options = webdriver.FirefoxOptions()
            firefox_options.headless = True
            driver = webdriver.Firefox(firefox_options=firefox_options)

        elif browerType == "grid":
            # 通过远程节点运行
            chrome_capabilities = {
                "browserName": "chrome",
                "version": "",
                "platform": "ANY",
                "javascriptEnabled": True,
                # "marionette": True,
            }

            driver = webdriver.Remote(config.hubIP, desired_capabilities=chrome_capabilities)

            driver.set_window_size(1920, 1080)

        else:
            raise NameError("driver驱动类型定义错误！")
        return driver
# if __name__ == '__main__':
#     driver = Driver.get_driver()
#     driver.get('http://www.baidu.com')